package com.castle.fortress.admin.goods.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.goods.dto.GoodsTagDto;
import com.castle.fortress.admin.goods.entity.GoodsTagEntity;
import com.castle.fortress.admin.goods.service.GoodsTagService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品标签表 api 控制器
 *
 * @author majunjie
 * @since 2021-11-23
 */
@Api(tags="商品标签表api管理控制器")
@RestController
@RequestMapping("/api/goods/goodsTag")
public class ApiGoodsTagController {
    @Autowired
    private GoodsTagService goodsTagService;


    /**
     * 商品标签表的分页展示
     * @param goodsTagDto 商品标签表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("商品标签表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<GoodsTagDto>> pageGoodsTag(GoodsTagDto goodsTagDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<GoodsTagDto> page = new Page(pageIndex, pageSize);

        IPage<GoodsTagDto> pages = goodsTagService.pageGoodsTag(page, goodsTagDto);
        return RespBody.data(pages);
    }

    /**
     * 商品标签表保存
     * @param goodsTagDto 商品标签表实体类
     * @return
     */
    @ApiOperation("商品标签表保存")
    @PostMapping("/save")
    public RespBody<String> saveGoodsTag(@RequestBody GoodsTagDto goodsTagDto){
        if(goodsTagDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsTagEntity goodsTagEntity = ConvertUtil.transformObj(goodsTagDto,GoodsTagEntity.class);
        if(goodsTagService.save(goodsTagEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品标签表编辑
     * @param goodsTagDto 商品标签表实体类
     * @return
     */
    @ApiOperation("商品标签表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateGoodsTag(@RequestBody GoodsTagDto goodsTagDto){
        if(goodsTagDto == null || goodsTagDto.getId() == null || goodsTagDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsTagEntity goodsTagEntity = ConvertUtil.transformObj(goodsTagDto,GoodsTagEntity.class);
        if(goodsTagService.updateById(goodsTagEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品标签表删除
     * @param ids 商品标签表id集合
     * @return
     */
    @ApiOperation("商品标签表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteGoodsTag(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(goodsTagService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品标签表详情
     * @param id 商品标签表id
     * @return
     */
    @ApiOperation("商品标签表详情")
    @GetMapping("/info")
    public RespBody<GoodsTagDto> infoGoodsTag(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsTagEntity goodsTagEntity = goodsTagService.getById(id);
            GoodsTagDto goodsTagDto = ConvertUtil.transformObj(goodsTagEntity,GoodsTagDto.class);
        return RespBody.data(goodsTagDto);
    }


}
